package com.boss.example.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import com.boss.example.entity.dto.RolesDTO;
import com.boss.example.entity.dto.UserDTO;
import com.boss.example.entity.vo.RolesVO;
import com.boss.example.entity.vo.UserVO;
import com.boss.example.entity.vo.CommonResult;
import com.boss.example.service.RolesService;

/**
 * @author Hu_liantian
 * @date 2020/8/6
 */
@Slf4j
@RestController
@RequestMapping("/role")
public class RolesController {

    @Resource
    private RolesService rolesService;

    @PostMapping("/insert")
    public CommonResult insertRole(@RequestBody RolesVO roleVO){
        RolesDTO rolesDTO = new RolesDTO();
        BeanUtils.copyProperties(roleVO,rolesDTO);
        log.info(rolesDTO.toString());
        RolesDTO result = rolesService.insertRole(rolesDTO);
        if(result != null){
            return new CommonResult(200,"插入成功",result);
        }else{
            return new CommonResult(400,"插入失败",null);
        }
    }

    @PostMapping("/delete")
    public CommonResult deleteRole(@RequestBody RolesVO roleVO){
        RolesDTO rolesDTO = new RolesDTO();
        BeanUtils.copyProperties(roleVO,rolesDTO);
        int result = rolesService.deleteRole(rolesDTO);
        if(result>0){
            return new CommonResult(200,"删除成功",result);
        }else{
            return new CommonResult(400,"删除失败",null);
        }
    }

    @PostMapping("/updateName")
    public CommonResult updateRolesName(@RequestBody RolesVO roleVO){
        RolesDTO rolesDTO = new RolesDTO();
        BeanUtils.copyProperties(roleVO,rolesDTO);
        int result = rolesService.updateRolesName(rolesDTO);
        if(result>0){
            return new CommonResult(200,"更新成功",result);
        }else{
            return new CommonResult(400,"更新失败",null);
        }
    }

    @GetMapping("/getAll")
    public CommonResult getAllRoles(){
        List<RolesDTO> rolesDTOList = rolesService.getAllRoles();
        if(rolesDTOList != null ){
            List<RolesVO> roleVOList = new ArrayList<>();
            BeanUtils.copyProperties(rolesDTOList,roleVOList);
            return new CommonResult(200,"查询成功",roleVOList);
        }else{
            return new CommonResult(400,"查询失败",null);
        }
    }

    @GetMapping("/getUserRoles")
    public CommonResult getUserRoles(@RequestBody UserVO usersVO){
        UserDTO usersDTO = new UserDTO();
        BeanUtils.copyProperties(usersVO,usersDTO);
        if(usersDTO != null ){
            List<RolesDTO> rolesDTOList = rolesService.getUserRoles(usersDTO.getUserId());

            return new CommonResult(200,"该用户的所有角色",rolesDTOList);
        }else{
            return new CommonResult(400,"查询失败",null);
        }
    }

}
